package com.cashbook.service.impl;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.cashbook.mapper.TbArticleMapper;
import com.cashbook.pojo.TbArticle;
import com.cashbook.pojo.TbArticleExample;
import com.cashbook.pojo.TbArticleExample.Criteria;
import com.cashbook.service.ArticleService;

import entity.PageResult;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class ArticleServiceImpl implements ArticleService {

	@Autowired
	private TbArticleMapper articleMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<TbArticle> findAll() {
		return articleMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<TbArticle> page=   (Page<TbArticle>) articleMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(TbArticle article) {
		articleMapper.insert(article);		
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(TbArticle article){
		articleMapper.updateByPrimaryKey(article);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public TbArticle findOne(Long id){
		return articleMapper.selectByPrimaryKey(id);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Long[] ids) {
		for(Long id:ids){
			articleMapper.deleteByPrimaryKey(id);
		}		
	}
	
	
		@Override
	public PageResult findPage(TbArticle article, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		TbArticleExample example=new TbArticleExample();
		Criteria criteria = example.createCriteria();
		
		if(article!=null){			
			if(article.getCover()!=null && article.getCover().length()>0){
				criteria.andCoverLike("%"+article.getCover()+"%");
			}
			if(article.getTitle()!=null && article.getTitle().length()>0){
				criteria.andTitleLike("%"+article.getTitle()+"%");
			}
			if(article.getCateId()!=null) {
				criteria.andCateIdEqualTo(article.getCateId());
			}
		}
		
		Page<TbArticle> page= (Page<TbArticle>)articleMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}

		@Override
		public List<TbArticle> findBycriteria(String cateId, String beginTime, String endTime, String title) throws Exception {
			TbArticleExample example=new TbArticleExample();
			Criteria criteria = example.createCriteria();
			SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
					//如果双方都为null 不添加日期条件
					/*if(beginTime != null || endTime !=null ) {//如果起始日期和截止日期都 不为null 或者 空
						if(!"".equals(beginTime) && !"".equals(endTime)) {
							//添加日期区间查询条件
							criteria.andTimeBetween(sm.parse(beginTime), sm.parse(endTime));
						}else {//如果某一方不为null或者为空
							if(beginTime != null && !"".equals(beginTime) ) {//如果起始日期不为 null 或者 为空
								criteria.andTimeGreaterThanOrEqualTo(sm.parse(beginTime));
							}
							if(endTime !=null && !"".equals(endTime)) {//如果结束日期不为 null 或者 为空
								criteria.andTimeLessThanOrEqualTo(sm.parse(endTime));
							}
						}
					}*/
			if(!"".equals(beginTime) && beginTime!=null && !"".equals(endTime) && endTime!=null) {
				criteria.andTimeBetween(sm.parse(beginTime), sm.parse(endTime));

			}else if(!"".equals(beginTime) && beginTime!=null && ("".equals(endTime)) ) {
				criteria.andTimeGreaterThanOrEqualTo(sm.parse(beginTime));
			}else if(!"".equals(endTime) && endTime!=null && (beginTime=="")) {
				criteria.andTimeLessThanOrEqualTo(sm.parse(endTime));

			}
				
				
				if(cateId!=null && cateId!=""){
					criteria.andCateIdEqualTo(Long.parseLong(cateId));
				}
			
				if(!"".equals(title)&&title!=null) {
					criteria.andTitleLike("%"+title+"%");
				}
				
			return articleMapper.selectByExample(example);
		}
		/*@Test
		public void test1() throws ParseException {
			SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
			
			System.out.println(parse);
		}
		*/

		@Override
		public List<TbArticle> findByCateId(Long cateId) {
			TbArticleExample example=new TbArticleExample();
			Criteria criteria = example.createCriteria();
			if(cateId !=null) {
				criteria.andCateIdEqualTo(cateId);
			}
			return articleMapper.selectByExample(example);
		}
	
}
